﻿using System;
using System.Collections.Generic;
using Evpo.ContactData.Common;
using System.Data.SqlClient;
using System.Data;

namespace Evpo.ContactData.Data
{
    public class ExportAdapter
    {
        public ExportAdapter(Connection conn)
        {
            _conn = conn;
        }

        #region Fields
        private readonly Connection _conn;
        #endregion

        #region Properties
        #endregion

        #region Methods
        public DataTable FillDataTable(int exportID)
        {
            DataTable retVal = new DataTable();
            Export exportRow = GetExportRow(exportID);
            
            try
            {
                using (SqlCommand cmd = new SqlCommand(exportRow.SProc, _conn.SqlConn))
                {
                    _conn.Open();
                    cmd.CommandType = CommandType.StoredProcedure;
                    using (SqlDataReader reader = cmd.ExecuteReader())
                    {
                        retVal.Load(reader);
                    }
                }
            }
            finally
            {
                if (_conn.IsOpen)
                    _conn.Close();
            }

            return retVal;
        }

        public List<Export> GetExportTable()
        {
            List<Export> retVal = new List<Export>();
            using (ProcedureOutputReader procReader = new ProcedureOutputReader(_conn, SProcs.GetExportTable))
            using (SqlDataReader reader = procReader.CreateReader())
            {
                while (reader.Read())
                {
                    retVal.Add(FillExport(procReader));
                }
            }

            return retVal;
        }

        private Export GetExportRow(int id)
        {
            Export retVal = null;
            SqlParameter parameter = new SqlParameter("@ID", id);
            using (ProcedureOutputReader procReader = 
                new ProcedureOutputReader(_conn, SProcs.GetExportRow, new SqlParameter[]{ parameter }))
            using (SqlDataReader reader = procReader.CreateReader())
            {
                if (!reader.Read())
                {
                    throw new ApplicationException("Export with id '" + id.ToString() + "' is not found");
                }

                retVal = FillExport(procReader);
            }

            return retVal;
        }

        private Export FillExport(ProcedureOutputReader procReader)
        {
            return new Export()
            {
                ID = procReader.GetID(Fields.ID),
                DisplayName = procReader.GetString(Fields.DisplayName),
                SProc = procReader.GetString(Fields.SProc)
            };
        }


        #endregion Methods

        #region Classes
        private class Fields
        {
            internal const string ID = "ID";
            internal const string DisplayName = "DisplayName";
            internal const string SProc = "SProc";
        }
        #endregion Classes
    }
}
